package utils;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.SequenceFile;

import java.io.FileWriter;
import java.io.IOException;
import java.util.Random;

/**
 * @Author lixinlei
 * @Date 2023/3/17 9:14
 */
public class GenerateData {

    static Random random = new Random();

    /**
     * 生成序列化类型的数据
     */
    public static void generateSequenceData() throws Exception {

        Configuration conf = new Configuration();
        FileSystem fs = FileSystem.get(conf);
        //创建一个写序列化文件的对象
        SequenceFile.Writer writer = SequenceFile.createWriter(
                fs,
                conf,
                new Path("file:///D:/data/data_hadoop/InputFormat/input_seq/temperature2.seq"),
                IntWritable.class,
                IntWritable.class
        );

        IntWritable outKey = new IntWritable();
        IntWritable outValue = new IntWritable();

        //随机生成100条的测试数据，key是2000-2020的年份，value是-40到40摄氏度的温度值
        for (int i = 0; i < 100; i++) {
            outKey.set(random.nextInt(21)+2000);
            outValue.set(random.nextInt(81)-40);
            writer.append(outKey,outValue);
        }

        writer.hflush();
        writer.close();


    }

    /**
     * 生成 年月 对应 温度的数据
     * 数据格式
     *      2022-01 20
     *      2022-03 16
     *      ...
     */
    public static void generateYearMonthTempData() throws IOException {

        String year = "2022-";

        FileWriter fw = new FileWriter("D:/data/data_hadoop/temperatureMonthCount/input/temperature.txt");

        for (int i = 0; i < 1000; i++) {
            //随机一个月份
            int month = random.nextInt(12)+1;
            //随机一个温度值
            int temperature = random.nextInt(81)-40;

            String singleData = year+month+"\t"+temperature+"\n";

            if(month<10){
                singleData = singleData.replace("2022-","2022-0");
            }

            fw.write(singleData);
        }

        fw.flush();
        fw.close();
    }

    /**
     * 生成人员分组的数据
     * 数据格式
     *      group-07    asdfg   20  男
     */
    public static void generatePersonGroupData() throws IOException {

        String groupHeader = "group-";

        FileWriter fw = new FileWriter("D:/data/data_hadoop/genderGroup/input/gender.txt");

        String[] genders = {"男","女"};

        for (int i = 0; i < 1000; i++) {

            //随机生成小组编号
            String groupNum = groupHeader+String.format("%02d",random.nextInt(20)+1);

            //随机生成的姓名
            StringBuffer sb = new StringBuffer();
            for (int j = 0; j < 5; j++) {
                sb.append((char)(random.nextInt(26)+97));
            }

            //随机一个18-30岁的年龄
            int age = random.nextInt(13)+18;

            //随机一个性别
            String gender = genders[random.nextInt(2)];

            fw.write(groupNum+"\t"+sb.toString()+"\t"+age+"\t"+gender+"\n");
        }

        fw.flush();
        fw.close();

    }

    /**
     * 生成年份和温度的数据
     * 数据格式
     *      2000    10
     *      ...
     */
    public static void generateYearTempData() throws IOException {

        FileWriter fw = new FileWriter("D:/data/data_hadoop/temperatureCount/input/temperature.txt");

        for (int i = 0; i < 100000; i++) {

           int year = random.nextInt(101) + 1900;

           int temp = random.nextInt(81) - 40;

           fw.write(year+"\t"+temp+"\n");

        }

        for (int i = 0; i < 900000; i++) {

            int year = random.nextInt(21) + 2000;

            int temp = random.nextInt(81) - 40;

            fw.write(year+"\t"+temp+"\n");

        }

        fw.flush();
        fw.close();
    }

    public static void generateYearTempSeqData() throws IOException {

        Configuration conf = new Configuration();

        FileSystem fs = FileSystem.get(conf);

        SequenceFile.Writer writer = SequenceFile.createWriter(
                fs,
                conf,
                new Path("D:/data/data_hadoop/temperatureCount/input/temperature.seq"),
                IntWritable.class,
                IntWritable.class
        );


        IntWritable outKey = new IntWritable();
        IntWritable outValue = new IntWritable();

        for (int i = 0; i < 100000; i++) {

            int year = random.nextInt(101) + 1900;

            int temp = random.nextInt(81) - 40;

            outKey.set(year);
            outValue.set(temp);

            writer.append(outKey,outValue);

        }

        for (int i = 0; i < 900000; i++) {

            int year = random.nextInt(21) + 2000;

            int temp = random.nextInt(81) - 40;

            outKey.set(year);
            outValue.set(temp);

            writer.append(outKey,outValue);

        }

        writer.hflush();
        writer.close();
    }

    public static void main(String[] args) throws Exception {
//        generateSequenceData();
//        generateYearMonthTempData();
//        generatePersonGroupData();
//        generateYearTempData();
        generateYearTempSeqData();
    }
}
